var apiHost = wx.getStorageSync('apiHost');
var yunId = "1";
var openId = wx.getStorageSync('openId');
var detailList = "";
var start = 0;
var length = 10;
var sign = "1";
var limitTime = "";
var types = "";
var orderNo = "";
var phone = "";
var flag = "";
var searchList = null;
var showNullMsg = false;
var list = new Array();
var util = require('../../utils/util.js');
var leftTime = new Array();
var sendleftTime = new Array();
Page({
  /**
  * 页面的初始数据
  */
  data: {
    hiddenLoading: true,    //loading
    showNullMsg: false
  },
  /**
  * 生命周期函数--监听页面加载
  */
  onLoad: function (options) {
    var that = this;
    //显示加载动画
    that.setData({
      hiddenLoading: false
    });
    that.orderDetail();
    // flag = options.flag;
    // that.searchOrder(event);
    /**
         * 获取系统信息
         */
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight,
          hiddenLoading: true
        });
      }
    });
  },
  contact: function (event) {
    var phone = event.currentTarget.dataset.phone;
    wx.makePhoneCall({
      phoneNumber: phone //仅为示例，并非真实的电话号码
    })
  },
  // 页面渲染完成后 调用  
  onReady: function () {
    var interval = setInterval(function () {
      var that = this;
      leftTime = new Array();
      var nowTime = util.formatTime(new Date()).toString();
      var Nhour = nowTime.substring(11, 13) * 1;
      var Nmin = nowTime.substring(14, 16) * 1;
      var Nsec = nowTime.substring(17, 19) * 1;
      types = "2"; 
      for (var i = 0; i < list.length; i++) {
        if(list[i].state == "1"){
          // console.log("!!!!");
          var payTime = list[i].payTime;
          
          orderNo = list[i].orderNo;
          var createtime = list[i].createTime;
          if (list[i].payType == "0") {
            var Emin = createtime.substring(14, 16) * 1 + limitTime * 1;
            var Ehour = createtime.substring(11, 13) * 1;
            var Esec = createtime.substring(17, 19) * 1;
          } else {
            var Emin = payTime.substring(14, 16) * 1 + limitTime * 1;
            var Ehour = payTime.substring(11, 13) * 1;
            var Esec = payTime.substring(17, 19) * 1;
          }
          if (Emin > 60) {
            Emin = Emin - 60;
            Ehour = Ehour + 1;
          }
          var Lmin = 0;
          var Lsec = 0;
          var Lhour = 0;
          if (Esec >= Nsec) {
            Lsec = Esec - Nsec;
          } else {
            Esec = Esec + 60;
            Lsec = Esec - Nsec;
            Emin = Emin - 1;
          }
          if (Emin >= Nmin) {
            Lmin = Emin - Nmin;
          } else {
            Emin = Emin + 60 ;
            Lmin = Emin - Nmin;
            Ehour = Ehour - 1;
          }

          //当前时间是否大于过期时间判断
          var flags = false;
          if(Nhour>Ehour){
            flags = true;
          }
          if(Nhour == Ehour){
            if(Nmin > Emin){
              flags = true;
            }
            else if(Nmin == Emin){
              if(Nsec> Esec){
                flags = true;
              }
            }
          }
          Lhour = Ehour - Nhour;
          // console.log(Lmin);
          if ((Lmin == 0 && Lsec == 0) || flags) {
            wx.request({
              url: apiHost + 'xcx/order/confirmAcceptOrder',
              data: {
                yunId: yunId,
                openId: openId,
                orderNo: orderNo,
                state: "0",
                'type': types
              },
              header: {
                'conttent-type': 'application-json'
              },
              success: function (res) {
                // console.log("after cancel is");
                // console.log(res);
                that.orderDetail();
              }
            })
          } else {
            if (Ehour>=Nhour && Emin>=Nmin) {
              leftTime.push(Lmin + "分" + Lsec + "秒");
              that.setData({
                leftTime: leftTime
              })
            }
          }
         
        }else{
          leftTime.push("");
          that.setData({
            leftTime: leftTime
          })
        }
      }
    }.bind(this), 1000);
    //配送中倒计时
    var sendInterval = setInterval(function () {
      var that = this;
      sendleftTime = new Array();
      var nowTime = util.formatTime(new Date()).toString();
      var Nhour = nowTime.substring(11, 13) * 1;
      var Nmin = nowTime.substring(14, 16) * 1;
      var Nsec = nowTime.substring(17, 19) * 1;
      for (var i = 0; i < list.length; i++) {
        if(list[i].state == "2"){
          var acceptTime = list[i].acceptTime;
          var deliveryLimitTime = list[i].deliveryLimitTime;
          orderNo = list[i].orderNo;
          var Emin = acceptTime.substring(14, 16) * 1 ;
          var Ehour = acceptTime.substring(11, 13) * 1 + deliveryLimitTime * 1;
          var Esec = acceptTime.substring(17, 19) * 1;
          // if (Emin > 60) {
          //   Emin = Emin - 60;
          //   Ehour = Ehour + 1;
          // }
          var Lmin = 0;
          var Lsec = 0;
          var Lhour = 0;
          if (Esec >= Nsec) {
            Lsec = Esec - Nsec;
          } else {
            Esec = Esec + 60;
            Lsec = Esec - Nsec;
            Emin = Emin - 1;
          }
          if (Emin >= Nmin) {
            Lmin = Emin - Nmin;
          } else {
            Emin = Emin + 60
            Lmin = Emin - Nmin;
            Ehour = Ehour - 1;
          }
          if (Ehour >= Nhour) {
            Lhour = Ehour - Nhour;
            if (Lhour <= deliveryLimitTime * 1) {
              sendleftTime.push(Lhour + "时" + Lmin + "分" + Lsec + "秒");
              that.setData({
                sendleftTime: sendleftTime
              })
            }
          }else{
            sendleftTime.push("您的订单将在12小时内将自动确认收货");
            that.setData({
              sendleftTime: sendleftTime
            })
          }
        }else{
          sendleftTime.push("");
          that.setData({
            sendleftTime: sendleftTime
          })
        }
      }
    }.bind(this), 1000);
  },
  // 删除dingdan
  delOrder:function(event){
    var that = this;
    orderNo = event.currentTarget.dataset.reforderno;
    that.setData({
      showModal: true,
      delOrderNo: orderNo
    })
  },
  toDetail:function(event){
    var orderNo = event.currentTarget.dataset.orderNo;
    var flag = "list";
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      mask: true
    })
    wx.navigateTo({
      url: '/pages/ordersDetail/myOrders?orderNo=' + orderNo + "&yunId=" + yunId + "&openId=" + openId + "&flag="+flag,
    })
  },
  cancel:function(event){
    this.setData({
      showModal: false
    })
  },
  confirm: function (event) {
    var delorderNo = event.currentTarget.dataset.delorderno;
    var that = this;
    wx.request({
      url: apiHost + 'xcx/order/del',
      data: {
        yunId: yunId,
        orderNo: delorderNo,
        openId: openId
      },
      header: {
        'conttent-type': 'application-json'
      },
      success: function (res) {
        /**
         * 获取系统信息
         */
        wx.getSystemInfo({
          success: function (res) {
            that.setData({
              hiddenLoading: true
            });
          }
        });

        if (res.data.code == "10000") {
          wx.showToast({
            title: '删除成功!',
            icon: 'success',
            duration: 2000
          })
          // 跳转订单列表
          wx.navigateTo({
            url: '/pages/order/order',
            data: {
              yunId: yunId,
              openId: openId
            }
          })
        }
      }
    })
  },
  refuse: function (event) {
    var that = this;
    var state = 0;
    types = '0';
    var reforderno = event.currentTarget.dataset.reforderno;
    //显示加载动画
    that.setData({
      hiddenLoading: false
    });
    wx.request({
      url: apiHost + 'xcx/order/confirmAcceptOrder',
      data: {
        yunId: yunId,
        state: state,
        orderNo: reforderno,
        openId: openId,
        'type': types
      },
      header: {
        'conttent-type': 'application-json'
      },
      success: function (res) {
        // console.log(res);
        /**
         * 获取系统信息
         */
        wx.getSystemInfo({
          success: function (res) {
            that.setData({
              hiddenLoading: true
            });
          }
        });
        if (res.data.code == "10000") {
          that.orderDetail();
        }
      },
    })
  },
  accept: function (event) {
    var state = 1;
    var that = this;
    //显示加载动画
    that.setData({
      hiddenLoading: false
    });
    // console.log(event);
    var acOrderNo = event.currentTarget.dataset.acorderno;
 
    wx.request({
      url: apiHost + 'xcx/order/confirmAcceptOrder',
      data: {
        yunId: yunId,
        state: state,
        orderNo: acOrderNo,
        openId: openId,
        'type':'3'
      },
      header: {
        'conttent-type': 'application-json'
      },
      success: function (res) {
        // console.log(res);
        /**
         * 获取系统信息
         */
        wx.getSystemInfo({
          success: function (res) {
            that.setData({
              hiddenLoading: true
            });
          }
        });
        if (res.data.code == "10000") {
          that.orderDetail();
        }
      },
    })
  },
  // onPullDownRefresh: function () {
  //   this.onLoad()
  // },
  onPullDownRefresh: function () {
    var that = this;
    //显示加载动画
    that.setData({
      hiddenLoading: false
    });
    that.onLoad()
    /**
         * 获取系统信息
         */
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          hiddenLoading: true
        });
      }
    });
  },
  searchOrder:function(event){
    var that = this;
    var schOrderNo = event.detail.value;
    //显示加载动画
    that.setData({
      hiddenLoading: false
    });
    wx.request({
      url: apiHost + 'xcx/order/searchList',
      data: {
        yunId: yunId,
        openId: openId,
        start: start,
        length: length,
        sign: sign,
        orderNo: schOrderNo
      },
      header: {
        'conttent-type': 'application-json'
      },
      success: function (res) {
        /**
         * 获取系统信息
         */
        wx.getSystemInfo({
          success: function (res) {
            that.setData({
              hiddenLoading: true
            });
          }
        });
        searchList = res.data.data.list; 
        
        if (searchList.length == 0){
          showNullMsg = true;
          that.setData({
            showNullMsg: showNullMsg,
            detailList: searchList
          })
        }else{
          that.setData({
            showNullMsg:false,
            detailList: res.data.data
          })
        }
        that.setData({
          searchList: res.data.data
        })
      }
    })
  },
  orderDetail:function(){
    var that = this;
    //显示加载动画
    that.setData({
      hiddenLoading: false
    });
    wx.request({
      url: apiHost + 'xcx/order/searchList',
      data:{
        yunId: yunId,
        openId: openId,
        start: start,
        length: length,
        sign: sign
      },
      header: {
        'conttent-type': 'application-json'
      },
      success:function(res){
        console.log(res);
        /**
         * 获取系统信息
         */
        wx.getSystemInfo({
          success: function (res) {
            that.setData({
              hiddenLoading: true
            });
          }
        });
        list = res.data.data.list;
        limitTime = res.data.data.limitTime;
        phone = res.data.data.phone
        // that.getleftTime(list);
        that.setData({
          detailList:res.data.data,
          phone: phone
        })
      }
    })
  },
})